﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web.Script.Serialization;
using System.Web.Services;

namespace WSDemo
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ExpenseQueue : System.Web.Services.WebService
    {
        #region -= Demo Expense Class =-

        private class Expense
        {
            public int ID { get; set; }
            public int UserID { get; set; }
            public DateTime CreatedTS { get; set; }
            public double TotalAmount { get; set; }
            public Expense ParentExpense { get; set; }
        }

        private static IList<Expense> GetTestExpenses()
        {
            var list = new List<Expense>();
            list.Add(new Expense { ID = 1, UserID = 6969, CreatedTS = new DateTime(2008, 7, 1), TotalAmount = 999 });
            list.Add(new Expense { ID = 2, UserID = 5721, CreatedTS = new DateTime(1999, 9, 9), TotalAmount = 888 });
            list.Add(new Expense { ID = 3, UserID = 1001, CreatedTS = new DateTime(2001, 8, 8), TotalAmount = 777 });

            return list;
        }

        private static DataTable ListToTable(IList<Expense> list)
        {
            if (null == list)
                return null;

            var table = new DataTable("ExpenseQueue");
            table.Columns.AddRange
            (
                new DataColumn[]
                {
                    new DataColumn("ID", typeof(int)),
                    new DataColumn("UserID", typeof(int)),
                    new DataColumn("CreatedTS", typeof(DateTime)),
                    new DataColumn("TotalAmount", typeof(double))
                }
            );

            if (0 == list.Count)
                return table;

            foreach (var item in list)
                table.Rows.Add(item.ID, item.UserID, item.CreatedTS, item.TotalAmount);

            return table;
        }

        #endregion

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string GetJSONStringOfExpenseQueue()
        {
            //The web method demo, using JSON.
            // Pros: lightweighted, simple, fast object de-serialization.

            //Get the test data, for the JSON demo, we use a IList type for the expense collection.
            var list = GetTestExpenses();
            if (null == list || 0 == list.Count)
                return null;


            var listToSerialize = from expense in list
                                  select new
                                  {
                                      ExpenseID = expense.ID,
                                      EmpID = expense.UserID,
                                      DateCreated = expense.CreatedTS, // will be serialized to long ticks.
                                      TotalExpense = expense.TotalAmount
                                  };

            var jsSerializer = new JavaScriptSerializer();
            jsSerializer.RecursionLimit = 3;
            return jsSerializer.Serialize(listToSerialize);
        }

        [WebMethod]
        public DataTable GetExpenseQueueDataTable()
        {
            //Get the test data.
            var list = GetTestExpenses();
            return ListToTable(list);
        }
    }
}
